package com.fruit.api.controller.fruit;

import com.fruit.bean.dto.LoginRespDto;
import com.fruit.bean.dto.WechatUserDto;
import com.fruit.bean.dto.app.UserRechargeDto;
import com.fruit.bean.dto.fruit.FruitMallConfigDto;
import com.fruit.bean.entity.fruit.FruitBannersInfo;
import com.fruit.bean.entity.system.User;
import com.fruit.bean.vo.front.Rets;
import com.fruit.service.fruit.FruitBannersService;
import com.fruit.service.system.UserService;
import com.fruit.utils.BigDecimalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 小程序相关系统接口
 */
@RestController
@RequestMapping("/fruit/app")
public class FruitSysController {

    @Value("${app.appname}")
    private String appName;

    @Autowired
    private FruitBannersService bannersService;

    @Autowired
    private UserService userService;

    /**
     * 获取商城相关的配置信息
     * @return
     */
    @GetMapping("/config")
    public Object getConfig(){
        FruitMallConfigDto configDto = new FruitMallConfigDto();
        configDto.setMallName(appName);
        return Rets.success(configDto);
    }

    /**
     * 获取轮播图
     * @return
     */
    @GetMapping("/banners")
    public Object getBanners(){
        List<FruitBannersInfo> bannersInfoList = bannersService.queryAll();
        return Rets.success(bannersInfoList);
    }

    /**
     * 微信登录
     */
    @PostMapping("login")
    public Object loginByWeixin(@RequestBody WechatUserDto wechatUser) {
        if(null == userService.findByAccount(wechatUser.getNickName())){
            // 用户信息不存在，保存用户信息
            User user = new User();
            //使用account存储用户名
            user.setAccount(wechatUser.getNickName());
            user.setSex(wechatUser.getGender());
            user.setAvatar(wechatUser.getAvatarUrl());
            user.setMoney(0D);
            user.setRoleid("7");
            //默认为普通用户
            user.setDeptid(7L);
            userService.insert(user);
        }
        LoginRespDto respDto = new LoginRespDto();
        respDto.setToken(UUID.randomUUID().toString());
        respDto.setUid(wechatUser.getNickName());
        return Rets.success(respDto);
    }

    @GetMapping("check-token")
    public Object checkToken(){
        return Rets.success();
    }

    /**
     * 获取用户信息
     * @param map 用户名
     * @return
     */
    @PostMapping("/user/info")
    public Object getUserInfo(@RequestBody Map<String,String> map){
        String name = map.get("name");
        WechatUserDto userDto = new WechatUserDto();
        User user = userService.findByAccount(name);
        if(user == null){
            return Rets.failure("没有找到对应的用户信息");
        }
        userDto.setUserId(user.getId());
        userDto.setNickName(user.getAccount());
        userDto.setAvatarUrl(user.getAvatar());
        userDto.setBalance(user.getMoney());
        userDto.setUserType(user.getRoleid());
        return Rets.success(userDto);
    }

    /**
     * 用户选择合作方式
     * @param map 用户Id,合作方式（8-果商，9-果农）
     * @return
     */
    @PostMapping("/user/type")
    public Object userSelectType(@RequestBody Map<String,String> map){
        Long userId = Long.valueOf(map.get("userId"));
        String type = map.get("type");
        User user = userService.get(userId);
        user.setRoleid(type);
        if(type.equals("8")){
            user.setDeptid(8L);
        }else if(type.equals("9")){
            user.setDeptid(9L);
        }
        userService.update(user);
        return Rets.success();
    }

    /**
     * 用户充值
     * @param rechargeDto 充值信息
     * @return
     */
    @PostMapping("/user/recharge")
    public Object userRecharge(@RequestBody UserRechargeDto rechargeDto){
        User user = userService.findByAccount(rechargeDto.getAcount());
        if (null == user) {
            //没有找到对应的用户信息
            return Rets.failure("没有找到对应的用户信息");
        }
        user.setMoney(BigDecimalUtils.add(user.getMoney(),rechargeDto.getRechargeAmount()).doubleValue());
        userService.update(user);
        return Rets.success();
    }

}
